var SignUpCtrl = dojo.declare(null, {
	//constructor/destructor
	constructor : signUpCtrl_constructor,
	destroy : signUpCtrl_destructor,
	
	//methods
	init : signUpCtrl_init
});

function signUpCtrl_constructor(){
	
}

function signUpCtrl_destructor(){
}

function signUpCtrl_init(){
	function showMsg(item, msg, type){
		$.validationEngine.buildPrompt(item, msg, type);
		setTimeout("$.validationEngine.closePrompt('" + item + "');", 10000);
	}
	
	errMsg = $("#signup_page_bound .page_variable div[var_name='errMsg']").html();
	if (errMsg != ""){
		if (errMsg == "too fast") showMsg("#form_signup #submit", "您的提交过于频繁，请稍候尝试。", "error");
		else if (errMsg == "registerCode error") showMsg("#input_register_code", "验证码错误（您可以尝试重新获取验证码）。", "error");
		else if (errMsg == "grade error") showMsg("#input_grade", "年级错误。", "error");
		else if (errMsg == "mobile error") showMsg("#signup_input_mobile", "手机号不合法，请检查您输入的手机号是否正确。", "error");
		else if (errMsg == "name error") showMsg("#input_name", "您输入的姓名不合法，请再次输入。", "error");
		else if (errMsg == "mobile registered") showMsg("#signup_input_mobile", "此手机号已被注册。如果您是得到Fido赠卡的会员，您的信息可能已被导入，您的初始密码为手机号后六位。如果您忘记了您的密码，请访问<a href='/login/forgetPassword'>这里</a>。", "error");
		else if (errMsg == "email registered") showMsg("#signup_input_email", "此邮箱已被注册。如果您是得到Fido赠卡的会员，您的信息可能已被导入，您的初始密码为手机号后六位。如果您忘记了您的密码，请访问<a href='/login/forgetPassword'>这里</a>。", "error");
		else if (errMsg == "email error") showMsg("#signup_input_email", "您输入的邮箱地址有误，请检查。", "error");
		else if (errMsg == "birthday error") showMsg("#input_birthday_year", "请检查您填入的出生日期是否正确。", "error");
	}
	lastGender = $("#signup_page_bound .page_variable div[var_name='last_signup_gender']").html();
	if (lastGender.indexOf("female") >= 0) $("#signup_page_bound #input_gender[value='female']").attr("checked", true);
	else if (lastGender.indexOf("male") >= 0) $("#signup_page_bound #input_gender[value='male']").attr("checked", true);
	
	$("#form_signup").validationEngine({scroll : false, unbindEngine : false});
	$("#form_signup #signup_mobile #require_code").click(function(){
		var mobile = $("#form_signup #signup_input_mobile").val();
		if (!isValidMobile(mobile)){
			showMsg("#form_signup #signup_input_mobile", "* 请输入合法的手机号。", "error");
			return;
		}
		showMsg("#form_signup #require_code", "正在请求，请稍候...", "load");
		$.getJSON("/login/sendRegisterCode", {mobile : mobile}, function(data){
			if (data == "too fast"){
				showMsg("#form_signup #require_code", "您的请求过于频繁，请稍候尝试。", "error");
			}  else if (data == "fail"){
				showMsg("#form_signup #require_code", "请求失败。", "error");
			}  else if (data == "registered"){
				showMsg("#form_signup #require_code", "此手机号已被注册。如果您是得到Fido赠卡的会员，您的信息可能已被导入，您的初始密码为手机号后六位。如果您忘记了您的密码，请访问<a href='/login/forgetPassword'>这里</a>。", "error");
			}  else if (data == "success"){
				showMsg("#form_signup #require_code", "我们已将注册码发到了您的邮箱：" + mobile + "@139.com，请注意查收邮件。", "pass");
			}  
		});
	});
	
	$("#form_signup #signup_email #require_code").click(function(){
		var email = $("#form_signup #signup_input_email").val();
		if (!isValidEmail(email)){
			showMsg("#form_signup #signup_input_email", "* 请输入合法的邮箱。", "error");
			return;
		}
		showMsg("#form_signup #require_code", "正在请求，请稍候...", "load");
		$.getJSON("/login/sendRegisterCode", {method : "email", email : email}, function(data){
			if (data == "too fast"){
				showMsg("#form_signup #require_code", "您的请求过于频繁，请稍候尝试。", "error");
			}  else if (data == "fail"){
				showMsg("#form_signup #require_code", "请求失败。", "error");
			}  else if (data == "registered"){
				showMsg("#form_signup #require_code", "此邮箱已被注册。如果您是得到Fido赠卡的会员，您的信息可能已被导入，您的初始密码为手机号后六位。如果您忘记了您的密码，请访问<a href='/login/forgetPassword'>这里</a>。", "error");
			}  else if (data == "success"){
				showMsg("#form_signup #require_code", "我们已将注册码发到了您的邮箱：" + email + "，请注意查收邮件。", "pass");
			}  
		});
	});
	
	$("#form_signup #submit").click(function(){$("#form_signup").submit();});
	
	lastBirthdayYear = $("#signup_page_bound .page_variable div[var_name='last_signup_birthday_year']").html();
	lastBirthdayMonth = $("#signup_page_bound .page_variable div[var_name='last_signup_birthday_month']").html();
	lastBirthdayDay = $("#signup_page_bound .page_variable div[var_name='last_signup_birthday_day']").html();
	for (var i = 2010; i >= 1990; i --)
		$("#form_signup #input_grade").append("<option value='" + i + "'>" + i + "</option>");
	lastGrade = $("#signup_page_bound .page_variable div[var_name='last_signup_grade']").html();
	if (!isNaN(lastGrade)) $("#form_signup #input_grade").val(parseInt(lastGrade));
	for (var i = 2005; i >= 1950; i --)
		$("#form_signup #input_birthday_year").append("<option value='" + i + "'>" + i + "</option>");
	function birthdayYearChange(){
		$("#input_birthday_month").html("");
		$("#input_birthday_month").append("<option value='--'>--</option>");
		$("#input_birthday_day").html("");
		$("#input_birthday_day").append("<option value='--'>--</option>");
		if ($("#input_birthday_year").val() != "--"){
			for (var i = 1; i <= 12; i ++)
				$("#form_signup #input_birthday_month").append("<option value='" + i + "'>" + i + "</option>");
		}
	}
	function birthdayMonthChange(){
		$("#input_birthday_day").html("");
		$("#input_birthday_day").append("<option value='--'>--</option>");
		if ($("#input_birthday_year").val() == "--") return;
		var year = parseInt($("#input_birthday_year").val());
		var month = parseInt($("#input_birthday_month").val());
		var dayCount = 31;
		if (month == 4 || month == 6 || month == 9 || month == 11) dayCount = 30;
		else if (month == 2){
			if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) dayCount = 29;
			else dayCount = 28;
		}
		for (var i = 1; i <= dayCount; i ++)
			$("#form_signup #input_birthday_day").append("<option value='" + i + "'>" + i + "</option>");
	}
	$("#form_signup #input_birthday_year").change(birthdayYearChange);
	$("#form_signup #input_birthday_month").change(birthdayMonthChange);
	if (!isNaN(lastBirthdayYear) && !isNaN(lastBirthdayMonth) && !isNaN(lastBirthdayDay)){
		$("#input_birthday_year").val(parseInt(lastBirthdayYear));
		birthdayYearChange();
		$("#input_birthday_month").val(parseInt(lastBirthdayMonth));
		birthdayMonthChange();
		$("#input_birthday_day").val(parseInt(lastBirthdayDay));
	}
	
	$.getJSON("/login/getSchools", function(data){
		$("#form_signup #signup_school #input_school").append("<option value='--'>请选择</option>");
		for (var i = 0; i < data.length; i ++)
			$("#form_signup #signup_school #input_school").append("<option value='" + data[i][0] + "'>" + data[i][1] + "</option>");
		lastSchool = $("#signup_page_bound .page_variable div[var_name='last_signup_school']").html();
		if (!isNaN(lastSchool)) $("#form_signup #input_school").val(parseInt(lastSchool));
	});
	$.getJSON("/login/getProfessions", function(data){
		$("#form_signup #signup_profession #input_profession").append("<option value='--'>请选择</option>");
		for (var i = 0; i < data.length; i ++)
			$("#form_signup #signup_profession #input_profession").append("<option value='" + data[i][0] + "'>" + data[i][1] + "</option>");
		lastProfession = $("#signup_page_bound .page_variable div[var_name='last_signup_profession']").html();
		if (!isNaN(lastProfession)) $("#form_signup #input_profession").val(parseInt(lastProfession));
	});
}

if (dojo.getObject("signUpCtrl") == null) var signUpCtrl = new SignUpCtrl;
$(document).ready(function(){
	signUpCtrl.init();
});

function validateMobile(){
	var m = $("#form_signup #signup_input_mobile").val();
	return isValidMobile(m);
}

function validateName(){
	var n = $("#form_signup #input_name").val();
	return isValidName(n);
}

function validateNewPass(){
	var password = $("#form_signup #input_password").val();
	return isValidPassword(password);
}

function validateRetypePass(){
	var pass1 = $("#form_signup #input_password").val();
	var pass2 = $("#form_signup #input_confirm_pass").val();
	return (pass1 == pass2);
}

function checkForm(){
	function showMsg(item, msg, type){
		$.validationEngine.buildPrompt(item, msg, type);
		setTimeout("$.validationEngine.closePrompt('" + item + "');", 10000);
	}
	if ($("#form_signup #input_birthday_year").val() == "--" || $("#form_signup #input_birthday_month").val() == "--" || $("#form_signup #input_birthday_day").val() == "--"){
		showMsg("#form_signup #input_birthday_year", "请填入您的生日。");
		return false;
	}
	if ($("#form_signup #input_grade").val() == "--"){
		showMsg("#form_signup #input_grade", "请填入您的年级。");
		return false;
	}
	if ($("#form_signup #input_school").val() == "--"){
		showMsg("#form_signup #input_school", "请填入您的学校。");
		return false;
	}
	if ($("#form_signup #input_profession").val() == "--"){
		showMsg("#form_signup #input_profession", "请填入您的专业。");
		return false;
	}
	return !$.validationEngine.submitValidation("#form_signup"); 
}